package cn.jdy.plugins.admin.export;


import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import cn.jdy.common.util.EncodeUtil;


/**
 * @desc   导出Excel
 * @author jga
 * @date   2016-7-14
 */
public class EvaluationXbliveExcelView extends  AbstractExcelView {
	@Override
    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Map> list = (List<Map>) model.get("dataSet");
        HSSFSheet sheet = workbook.createSheet("性别");
        sheet.setDefaultColumnWidth(12);
        HSSFCell cell = getCell(sheet,0,0);

        cell = getCell(sheet,0,0);
        setText(cell, "直播开始时间");
        cell = getCell(sheet,0,1);
        setText(cell, "直播结束时间");
        cell = getCell(sheet,0,2);
        setText(cell, "男性页面访问次数");
        cell = getCell(sheet,0,3);
        setText(cell, "女性页面访问次数");
        cell = getCell(sheet,0,4);
        setText(cell, "其他页面访问人数");
        
        for (int i = 0; i < list.size(); i++) {
        	HSSFRow row = sheet.createRow(i+1);
            Map<String,Object> obj = list.get(i);
            // 处理列
            row.createCell(0).setCellValue(obj.get("fromDate")+"");
            row.createCell(1).setCellValue(obj.get("toDate")+"");
            BigInteger bb=(BigInteger) obj.get("bb");
            BigInteger cc=(BigInteger) obj.get("cc");
            BigInteger dd=(BigInteger) obj.get("dd");
            row.createCell(2).setCellValue(Integer.valueOf(bb.intValue()));
            row.createCell(3).setCellValue(Integer.valueOf(cc.intValue()));
            row.createCell(4).setCellValue(Integer.valueOf(dd.intValue()));
        }
        String fileName = "性别统计";
        // 处理中文文件名
        
        response.setHeader("Cache-Control", "private");  
        response.setHeader("Pragma", "private");  
        response.setContentType("application/vnd.ms-excel;charset=utf-8");  
        response.setHeader("Content-Type", "application/force-download");  
          
        fileName = EncodeUtil.processFileName(request, fileName);  
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls"); 
        
//        response.setContentType("application/vnd.ms-excel");
//        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();

    }
	
	
}
